package Utilerias;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.LayoutStyle;

/**
 * @author Reynald0
 */
public class Splash extends JPanel
{
   private ImageIcon    imagen;  
   private JLabel       estados;
   private JProgressBar progreso;
   private GroupLayout  layout;
   
   public Splash()
   {
      this("");
   }
   
   public Splash(String pRutaImagen)
   {
      crearContenido();
      estados.setForeground(Color.BLUE);
      if (pRutaImagen.equals(""))
      {
         JOptionPane.showMessageDialog(this," Agrega la ruta de la imagen del \n"
                                          + " Splash por parametro");
         System.exit(0);
      }
      else
      {
         imagen = new ImageIcon(pRutaImagen); 
         setSize(imagen.getIconWidth(), imagen.getIconHeight());
      }
   }
   
   public void paintComponent(Graphics g)
   { 
      super.paintComponent(g); 
      g.drawImage(imagen.getImage(), 0, 0, imagen.getIconWidth(), imagen.getIconHeight(),this);
   }
   
   public void velocidadDeCarga(int pMiliSegundos) throws InterruptedException
   { 
      for(int i = 0; i<= 100; i++)
      {
         Thread.sleep(pMiliSegundos); 
         progreso.setForeground(Color.GREEN); 
         progreso.setValue(i);
         if(i == 1)
            estados.setText("Cargando elementos");
         else if(i == 20)
            estados.setText("Leyendo variales");
         else if(i == 40)
            estados.setText("Aplicando Tema");
         else if(i == 60)
            estados.setText("Estableciendo parametros");
         else if(i == 80)
            estados.setText("Abriendo programa");
      }
   }
   
   private void crearContenido()
   {
      progreso = new JProgressBar();
      estados = new JLabel();
      layout = new GroupLayout(this);
      setLayout(layout);
      layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(progreso, GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
                  .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(estados)
                        .addContainerGap())
            );
      layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addGroup(GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
                        .addGap(0,294,Short.MAX_VALUE)
                        .addComponent(estados)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(progreso, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
            );
   }
}
